#include "jabstractinputpanel_p.h"
#include "widgets/jvkbutton.h"

#include <QAbstractButton>
#include <QApplication>
#include <QTimerEvent>
#include <QScreen>

#include <functional>

namespace QTE
{

JAbstractInputPanelPrivate::JAbstractInputPanelPrivate() :
	m_backSpaceButton(new JVKButton())
{
	m_backSpaceButton->setIcon(QIcon(":/picture/back.png"));
	m_backSpaceButton->setIconSize(QSize(18,18));
	m_backSpaceButton->setText("←");

	m_backSpaceButton->setStyleSheet("background-color:rgb(50,50,50); color:white;");
	m_backSpaceButton->setFocusPolicy(Qt::NoFocus);
	m_backSpaceButton->setFixedSize(63,40);
}

JAbstractInputPanelPrivate::~JAbstractInputPanelPrivate()
{
	if( m_backSpaceButton->parent() != q_ptr )
		m_backSpaceButton->deleteLater();
}

void JAbstractInputPanelPrivate::init(JAbstractInputPanel *q_ptr)
{
	Q_ASSERT(q_ptr);
	setParent(q_ptr);

	this->q_ptr = q_ptr;
	m_backSpaceButton->setParent(q_ptr);

	QObject::connect(m_backSpaceButton, &JVKButton::pressed, this, [this]()
	{
		if( m_timerID < 0 )
			m_timerID = startTimer(600);
	});

	QObject::connect(m_backSpaceButton, &JVKButton::released, this, [this]()
	{
		if( m_timerID != -1 )
		{
			m_quickBackSpace = false;
			killTimer(m_timerID);
			m_timerID = -1;
		}
	});

	QObject::connect(m_backSpaceButton, &JVKButton::clicked, this, [this]()
	{
		if( m_quickBackSpace == false )
			this->q_ptr->backSpace();
	});
}

// 大小和位置自适应
static void __layoutAdaptation(QObjectList &list, qreal wRatio, qreal hRatio)
{
	for(auto &obj : list)
	{
		auto widget = J_OCT(QWidget*, obj);

		if( widget != nullptr )
		{
//			widget->setFixedSize(widget->width() * w_ratio, widget->height() * h_ratio);
			auto button = J_OCT(QAbstractButton*, widget);

			if( button != nullptr )
			{
				auto size = button->size();
				size.rwidth() *= wRatio;
				size.rheight() *= hRatio;

				button->setFixedSize(size);

				if( size.rwidth() > size.rheight() )
					size.rwidth() = size.rheight();
				else if( size.rheight() > size.rwidth() )
					size.rheight() = size.rwidth();

				button->setIconSize(size);
			}
			else
				__layoutAdaptation(J_CCT(QObjectList&, widget->children()), wRatio, hRatio);
		}
	}
}

QString JAbstractInputPanelPrivate::buttonNamePrefix() const
{
	return "j_tb_";
}

// 大小自适应
void JAbstractInputPanelPrivate::layoutAdaptation(qreal wRatio, qreal hRatio)
{
	__layoutAdaptation(J_CCT(QObjectList&, q_ptr->children()), wRatio, hRatio);
}

static void __mapperInit(const QObjectList &list, const QString &objNamePrefix,
						 std::function<void(QAbstractButton*)> &&handler)
{
	for(auto &obj : list)
	{
		auto widget = J_OCT(QWidget*, obj);
		if( widget == nullptr )
			continue;

		auto button = J_OCT(QAbstractButton*, widget);
		if( button == nullptr )
		{
			__mapperInit(widget->children(), objNamePrefix, std::move(handler));
			continue ;
		}

		else if( button->objectName().indexOf(objNamePrefix) >= 0 )
			handler(button);
	}
}

void JAbstractInputPanelPrivate::timerEvent(QTimerEvent *event)
{
	QObject::timerEvent(event);
	if( event->timerId() != m_timerID )
		return ;

	killTimer(m_timerID);
	m_timerID = startTimer(75);

	m_quickBackSpace = true;
	q_ptr->backSpace();
}

bool JAbstractInputPanelPrivate::eventFilter(QObject *watched, QEvent *event)
{
	auto button = J_OCT(QAbstractButton*, watched);

	if( button and event->type() == QEvent::MouseButtonRelease )
		emit q_ptr->keyRelease(button);

	return false;
}

void JAbstractInputPanelPrivate::mapperInit()
{
	__mapperInit(q_ptr->children(), buttonNamePrefix(), [this](QAbstractButton *button)
	{
		auto q = q_ptr;
		button->installEventFilter(this);

		QObject::connect(button, &QAbstractButton::pressed, q,
						 [q, button](){emit q->keyPressed(button);});

		QObject::connect(button, &QAbstractButton::clicked, q, [this, button]()
		{
			q_ptr->keyClicked(button);
			q_ptr->textInput(button->text());
		});
	});
}

} //namespace QTE
